package com.imooc.miaosha.dao;

import com.imooc.miaosha.domian.MiaoShaGoods;
import com.imooc.miaosha.vo.GoodsVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 模块描述: <br>
 * (商品持久化接口)
 *
 * @Author: Mr. xyb
 * @Date: 2019/4/18 23:33
 * @since: 1.8.0
 * @version: 1.0.0
 */
@Mapper
public interface GoodsDao {

    /**
     * 查询秒杀商品
     *
     * @return
     */
    @Select("select g.*, mg.stock_count, mg.start_date, mg.end_date, mg.miaosha_price from goods g left join miaosha_goods mg on g.id = mg.goods_id")
    List<GoodsVO> listGoodsVO();

    /**
     * 查询秒杀商品详情
     *
     * @param goodsId
     * @return
     */
    @Select("select g.*, mg.stock_count, mg.start_date, mg.end_date, mg.miaosha_price from goods g left join miaosha_goods mg on g.id = mg.goods_id where g.id = #{goodsId}")
    GoodsVO getGoodsVoByGoodsId(Long goodsId);

    /**
     * 秒杀扣库存
     *
     * @param miaoShaGoods
     * @return
     */
    @Update("update miaosha_goods set stock_count = stock_count - 1 where goods_id = #{goodsId} and stock_count > 0")
    int reduceStock(MiaoShaGoods miaoShaGoods);


    /**
     * 重置库存
     *
     * @param g
     * @return
     */
    @Update("update miaosha_goods set stock_count = #{stockCount} where goods_id = #{goodsId}")
    public int resetStock(MiaoShaGoods g);
}
